<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2018/12/15
 * Time: 14:01
 */
namespace app\console\controller;

use app\common\model\ApplyLog as Model;
use think\Controller;
use think\Db;
use think\Request;
use think\Url;

class ApplyLog extends Controller{

    public function index()
    {
        $get = request()->get();
        $id = Request::instance()->param('id');
        $param = Request::instance()->param();
        $model = (new Model())
            ->alias('a')
            ->join('member m','m.id = a.member_id','left')
            ->field('m.username as login,m.nickname,a.*')
            ->where('draw_id',$id);
        $list = $model
            ->paginate(15,false,['query'=>$get]);
        $count = $model->fetchSql(false)
            ->where('draw_id',$id)->count('id');

        //$draw = Db::name('draw')->order(['sort' => 'asc','probability' =>'asc','id' => 'asc'])->select();
        $draw = Db::name('draw_time_gift')->field('draw_title title,id')->where('draw_id',$id)->select();
        if(Request::instance()->isPost()){
            if(count($param['idd']) > $param['count']){
                fail('必中奖人数不能超过报名人数');
            }
            $array = [];
            $param = Request::instance()->param();

                for ($i=0;$i < count($param['idd']);$i++){
                    $array[$i]['id'] = $param['idd'][$i];
                    $array[$i]['draw_prize'] = $param['draw_prize'][$i];
                }
                $ret = (new Model())->saveAll($array);
                if($ret !== false){
                    win('奖品设置成功',Url::build('index',['id' => $id,'count' => $param['count']]));
                }


        }
        return $this->fetch('apply_log_index', [
            "mate_title" => '已报名人数列表',
            'data' => $list,
            'count' => $count,
            'draw' => $draw

        ]);
    }
    public function index2()
    {
        $get = request()->get();
        $list = (new Model())
            ->alias('a')
            ->join('member m','m.id = a.member_id','left')
            ->field('m.username as login,m.nickname,a.*')
            ->where('status',2)
            ->paginate(15,false,['query'=>$get]);
        $count = (new Model())->where('status',2)->count('id');
        $draw = Db::name('draw')->order(['sort' => 'asc','probability' =>'asc','id' => 'asc'])->select();
        return $this->fetch('apply_log_index', [
            "mate_title" => '已报名人数列表',
            'data' => $list,
            'count' => $count,
            'draw' => $draw

        ]);
    }

}